package cn.chenxing.module.analysis.controller;

import cn.chenxing.domain.dto.TrackTable;
import cn.chenxing.domain.threadlocal.ThreadData;
import cn.chenxing.domain.util.DialogBuilderUtil;
import cn.chenxing.module.analysis.service.AnalysisService;
import cn.chenxing.utils.CommonUtil;
import cn.chenxing.utils.TableUtil;
import com.jfoenix.controls.JFXComboBox;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Description 离群点检测控制层
 * @Author maogen.ymg
 * @Date 2020/4/16 19:25
 */
@Lazy
@FXMLController
@Slf4j
public class OutlierDetectController implements Initializable {

    @FXML
    private JFXComboBox<String> dataList;

    @FXML
    private JFXComboBox<String> userList;

    @FXML
    private TextField errorValue;

    @FXML
    private BorderPane borderPane;

    @Autowired
    private AnalysisService analysisService;

    private static final String QUERY_RESULT = "查询结果集";

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        dataList.getItems().addAll("文件数据集", QUERY_RESULT);
        dataList.setValue(dataList.getItems().get(0));

        CommonUtil.updateList(userList, true);

        // 加载已查询好的数据
        if(ThreadData.get().getTrackData() != null &&
                ThreadData.get().getTrackData().getOutlierDetectList() !=null) {
            dataList.setValue(ThreadData.get().getTrackData().getOutlierDataName());
            userList.setValue(ThreadData.get().getTrackData().getOutlierUserId());
            errorValue.setText(ThreadData.get().getTrackData().getErrorValue());
            TableUtil.importOutlierDetect(borderPane,ThreadData.get().getTrackData().getOutlierDetectList(), false);
        }

        // JFXComboBox监听
        dataList.getSelectionModel().selectedItemProperty().addListener(e ->
                userList.setDisable(QUERY_RESULT.equals(dataList.getSelectionModel().getSelectedItem())));
    }

    @FXML
    public void outlierDetect() {
        if(ThreadData.get().getTrackData() == null) {
            DialogBuilderUtil.showPositiveAlert(dataList, "无法进行分析", "还没导入数据，请选择文件菜单进行导入");
            return;
        }

        List<TrackTable> trackTableList;
        if(QUERY_RESULT.equals(dataList.getValue())) {
            if(ThreadData.get().getTrackData().getQueryResultList() != null) {
                trackTableList = ThreadData.get().getTrackData().getQueryResultList();
            }else {
                DialogBuilderUtil.showPositiveAlert(dataList, "无法分析", "您还未进行查询操作！");
                return;
            }
        } else if("所有用户".equals(userList.getValue())){
            // 整个数据
            trackTableList = CommonUtil.getTrackTableData();
        }else {
            trackTableList = CommonUtil.getTableDataByUserId(userList.getValue(), false);
        }

        if(trackTableList!=null) {
            // 保存相关数据
            ThreadData.get().getTrackData().setOutlierDataName(dataList.getValue());
            ThreadData.get().getTrackData().setOutlierUserId(userList.getValue());
            ThreadData.get().getTrackData().setErrorValue(errorValue.getText().trim());
            // 显示在表格上
            TableUtil.importOutlierDetect(borderPane,
                    analysisService.outlierDetect(trackTableList, Double.parseDouble(errorValue.getText().trim())), true);
        }

    }


}